package common;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.laoxinwen.app.R;

import java.util.HashMap;

import view.UserGuide;

/**
 * Created by sshong on 15/6/26.
 */
public class UserGuideUtil {

    public static final int GUIDE_MAIN_WA = 1;
    public static final int GUIDE_DETAIL_WA = 2;
    public static final int GUIDE_DETAIL_CAT = 3;
    public static final int GUIDE_TOPIC_H5 = 4;
    public static final int GUIDE_TOPIC_KEYNODE = 5;
    public static final int GUIDE_TOPIC_SORT = 6;
    public static final int GUIDE_TOPIC_FAVOR = 7;
    public static final int GUIDE_TOPIC_DISCUSS = 8;

    private static HashMap<String, UserGuideConfig> guideConfigMap;

    private static void init() {
        if (guideConfigMap != null) {
            return;
        }
        guideConfigMap = new HashMap<>();
        UserGuideConfig[] configs = {new UserGuideConfig(GUIDE_MAIN_WA, "点这里查看背后的故事", R.id.bt_show_topic, 1200),
                new UserGuideConfig(GUIDE_DETAIL_WA, "点这里查看背后的故事", R.id.bt_dig),
                new UserGuideConfig(GUIDE_DETAIL_CAT, "点这里能看到更多哦", R.id.bt_menu),
                new UserGuideConfig(GUIDE_TOPIC_H5, "点这里看大片", R.id.bt_h5),
                new UserGuideConfig(GUIDE_TOPIC_KEYNODE, "点这里展开时间轴", R.id.bt_keynode),
                new UserGuideConfig(GUIDE_TOPIC_SORT, "点这里对时间轴排序哦", R.id.bt_sort),
                new UserGuideConfig(GUIDE_TOPIC_FAVOR, "遇到感兴趣话题记得收藏哦，会自动推送最新进展给你", R.id.bt_care),
                new UserGuideConfig(GUIDE_TOPIC_DISCUSS, "看看网络上关于此事的各种讨论", R.id.tab_discuss)
        };
        for (UserGuideConfig config : configs) {
            guideConfigMap.put(String.valueOf(config.getGuide()), config);
        }
    }

    /**
     * 检查使用提示并自动显示
     *
     * @param guide
     * @param rootView
     * @return 引导显示成功返回true
     */
    public static boolean checkGuide(int guide, ViewGroup rootView) {
        return checkGuide(guide, rootView, null);
    }

    public static boolean checkGuide(int guide, ViewGroup rootView, View toView) {
        if (UserConfig.isGuided(guide)) {
            return false;
        }
        init();

        UserGuideConfig config = guideConfigMap.get(String.valueOf(guide));
        if (config == null) {
            return false;
        }

        UserGuide guideView = (UserGuide) LayoutInflater.from(rootView.getContext()).inflate(R.layout.user_guide, rootView, false);
        if (toView == null) {
            toView = rootView.findViewById(config.getToViewId());
        }
        guideView.setTip(toView, config.getTip(), true, config.getDelay());
        rootView.addView(guideView);

        UserConfig.setGuided(guide);
        return true;
    }

    private static class UserGuideConfig {
        private int guide;
        private String tip;
        private int toViewId;
        private int delay;

        public UserGuideConfig(int guide, String tip, int toViewId) {
            this(guide, tip, toViewId, 200);
        }

        public UserGuideConfig(int guide, String tip, int toViewId, int delay) {
            this.guide = guide;
            this.tip = tip;
            this.toViewId = toViewId;
            this.delay = delay;
        }

        public int getGuide() {
            return guide;
        }

        public String getTip() {
            return tip;
        }

        public int getToViewId() {
            return toViewId;
        }

        public int getDelay() {
            return delay;
        }
    }
}
